package com.alexsun.navcard.ui.home

import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.RecyclerView
import com.alexsun.navcard.R
import com.alexsun.navcard.entities.ShopItem


class ShopAdapter(private val dataSet: Array<ShopItem>, context: Context?) :
    RecyclerView.Adapter<ShopAdapter.ViewHolder>() {

    private val mContext: Context?

    init {
        mContext = context
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val titleView: TextView
        val descView: TextView
        val imageView: ImageView

        init {
            titleView = view.findViewById(R.id.text_title)
            descView = view.findViewById(R.id.text_desc)
            imageView = view.findViewById(R.id.shop_image)
        }
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.shop_item, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        viewHolder.titleView.text = dataSet[position].name
        viewHolder.descView.text = dataSet[position].description
        val drawable = ResourcesCompat.getDrawable(mContext!!.resources, R.drawable.foot_image, null)
        val bitmap = (drawable as BitmapDrawable).bitmap
        viewHolder.imageView.setImageBitmap(bitmap)
    }

    override fun getItemCount() = dataSet.size
}
